//
//  ZXZLoginViewController.m
//  sheyingzu
//
//  Created by tyu on 30.5.24.
//

#import "ZXZLoginViewController.h"


#import "ZXZRegisterViewController.h"

#import "ZXZDealViewController.h"

@interface ZXZLoginViewController ()<UITextViewDelegate,UIScrollViewDelegate>
@property(nonatomic,weak) UIScrollView *ScrollView;

@property(nonatomic,weak) UITextField *phoneTextF;
@property(nonatomic,weak) UITextField *CodeTextF;



@property(nonatomic,weak) UIButton *SelectedBtn;
@property(nonatomic,weak) UIButton *registBtn;


@end

@implementation ZXZLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)backBtnClick
{
    [self.navigationController popViewControllerAnimated:YES];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    
}


- (void)bac_addUI
{
    [super bac_addUI];
    
    [self setupView];
    
    [self setTreatyView];
    [self setupBottomView];
    
    [self.view bringSubviewToFront:self.navView];
    self.bacbBackBtn.hidden = YES;
    
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
    
}

- (void)setupView
{
    
    UIScrollView *ScrollV = [[UIScrollView alloc]init];
    [self.view addSubview:ScrollV];
    self.ScrollView =ScrollV;
    ScrollV.delegate = self;
    
    ScrollV.contentSize = CGSizeMake(ScreenWidth, ScreenHeight*1.1);
    ScrollV.contentInset = UIEdgeInsetsMake(0, 0, -10, 0);
    ScrollV.showsVerticalScrollIndicator = NO;
    ScrollV.frame = CGRectMake(0, 100, ScreenWidth, ScreenHeight-ShowDiff);
    
    
    CGFloat leftW = 30;
    
    UIButton *backBtn = [[UIButton alloc] init];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [self.ScrollView addSubview:backBtn];
    backBtn.frame = CGRectMake(30, 0, 25, 25);
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *titLab = [UIView lableWithText:@"打开运动视界" andColor:[UIColor blackColor] andFont:16];
    [self.ScrollView addSubview:titLab];
    titLab.font = [UIFont boldSystemFontOfSize:26];
    titLab.frame = CGRectMake(leftW, CGRectGetMaxY(backBtn.frame)+70, ScreenWidth-60, 30);
    
    
    UITextField *phoneTextF = [UIView textfieldPlaceholder:@"请输入手机号" keyboardType:UIKeyboardTypeNumberPad];
    [ScrollV addSubview:phoneTextF];
    self.phoneTextF =phoneTextF;
    
    phoneTextF.frame = CGRectMake(leftW, CGRectGetMaxY(titLab.frame)+30, ScreenWidth-40, 40);
    
    
    UIView *phoneV = [[UIView alloc]init];
    [ScrollV addSubview:phoneV];
    phoneV.backgroundColor = [UIView ColorWithHexString:@"#F4F4F4"];
    phoneV.frame = CGRectMake(leftW, CGRectGetMaxY(phoneTextF.frame)+2, ScreenWidth-40, 1);
    
    
    UITextField *CodeTextF = [UIView textfieldPlaceholder:@"请输入密码" keyboardType:UIKeyboardTypeDefault];
    [ScrollV addSubview:CodeTextF];
    self.CodeTextF =CodeTextF;
    CodeTextF.frame = CGRectMake(leftW, CGRectGetMaxY(phoneV.frame)+20, ScreenWidth-40, 40);
    
    UIView *codeV = [[UIView alloc]init];
    [ScrollV addSubview:codeV];
    codeV.backgroundColor = [UIView ColorWithHexString:@"#F4F4F4"];
    codeV.frame = CGRectMake(leftW, CGRectGetMaxY(CodeTextF.frame)+2, ScreenWidth-40, 1);
    
    
}


- (void)setTreatyView
{
    
    CGFloat leftW = 30;
    
    UIButton *SelectedBtn = [[UIButton alloc] init];
    [self.ScrollView addSubview:SelectedBtn];
    self.SelectedBtn = SelectedBtn;
    SelectedBtn.frame = CGRectMake(leftW, CGRectGetMaxY(self.CodeTextF.frame)+60, 20, 20);
    
    [SelectedBtn setBackgroundImage:[UIImage imageNamed:@"Treaty_Nor"] forState:UIControlStateNormal];
    [SelectedBtn setBackgroundImage:[UIImage imageNamed:@"Treaty_Sel"] forState:UIControlStateSelected];
    [SelectedBtn addTarget:self action:@selector(SelectedBtnDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UITextView *agreementTextView = [[UITextView alloc] init];
    [self.ScrollView addSubview:agreementTextView];
    
    agreementTextView.frame = CGRectMake(CGRectGetMaxX(SelectedBtn.frame)+5, CGRectGetMinY(SelectedBtn.frame)-7, ScreenWidth-40, 40);
    
    agreementTextView.font = [UIFont systemFontOfSize:14];
    agreementTextView.text = @"已阅读并同意<用户协议>和<隐私协议>";
    agreementTextView.backgroundColor = [UIColor clearColor];
    agreementTextView.delegate= self;
    //必须禁止输入，否则点击将弹出输入键
    agreementTextView.editable=NO;
    agreementTextView.scrollEnabled=NO;
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing= 1;
    NSDictionary*attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:14],
                                NSParagraphStyleAttributeName:paragraphStyle};
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:agreementTextView.text attributes:attributes];
    [attributedString addAttribute:NSLinkAttributeName value:@"yonghuxieyi://" range:NSMakeRange(6,6)];
    [attributedString addAttribute:NSLinkAttributeName value:@"yisizhengce://" range:NSMakeRange(13,6)];
    //    普通文字颜色
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIView ColorWithHexString:@"#999999"] range:NSMakeRange(0,agreementTextView.text.length)];
    agreementTextView.attributedText= attributedString;
    //设置被点击字体颜色
    agreementTextView.linkTextAttributes = @{NSForegroundColorAttributeName:[UIView ColorWithHexString:@"#07C6D2"]};
}

#pragma mark 富文本点击事件
-(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    if ([[URL scheme] isEqualToString:@"yonghuxieyi"]) {
        ZXZDealViewController *vc = [ZXZDealViewController new];
        vc.urlStr = userAgare;
        vc.titleStr = @"用户协议";
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([[URL scheme] isEqualToString:@"yisizhengce"]) {
        ZXZDealViewController *vc = [ZXZDealViewController new];
        vc.urlStr = userAgare;
        vc.titleStr = @"隐私政策";
        [self.navigationController pushViewController:vc animated:YES];
    }
    return YES;
}

- (void)setupBottomView
{
    
    CGFloat leftW = 30;
    
    UIButton *loginBtn = [[UIButton alloc] init];
    [self.ScrollView addSubview:loginBtn];
    loginBtn.frame = CGRectMake(leftW, CGRectGetMaxY(self.SelectedBtn.frame)+40, ScreenWidth-60, 44);
    
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginBtn.backgroundColor = [UIView ColorWithHexString:@"#07C6D2"];
    loginBtn.layer.cornerRadius = 44/2;
    loginBtn.layer.masksToBounds = YES;
    
    UIButton *registBtn = [[UIButton alloc] init];
    [self.ScrollView addSubview:registBtn];
    registBtn.frame = CGRectMake(0, CGRectGetMaxY(loginBtn.frame)+10, ScreenWidth, 40);
    
    NSString *str1 =@"没有账号? ";
    NSInteger length1 = [str1 length];
    NSString *nameStr =[NSString stringWithFormat:@"%@",@"去注册"];
    NSInteger Length2 = [nameStr length];
    NSString *str2 = [NSString stringWithFormat:@"%@%@", str1, nameStr];
    NSMutableAttributedString *str3 = [[NSMutableAttributedString alloc] initWithString: str2];
    
    [str3 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, str3.length)];
    //    默认颜色
    [str3 addAttribute:NSForegroundColorAttributeName value:[UIView ColorWithHexString:@"#999999"] range:NSMakeRange(0, length1)];
    //选中颜色
    [str3 addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(length1, Length2)];
    
    registBtn.titleLabel.numberOfLines = 0;
    [registBtn setAttributedTitle:str3 forState:UIControlStateNormal];
    //    是否文字居中
    registBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    [registBtn addTarget:self action:@selector(registBtnDidClicked) forControlEvents:UIControlEventTouchUpInside];
}

- (void)loginBtnClick
{
    if ([self.phoneTextF.text isEqualToString:defUserName]){
        
        if (![self.CodeTextF.text isEqualToString:defUserCode]){
            [NotifyLabel NotifyWithString:@"密码输入不正确"];
            return;
        }else{
            [self setUserData];
        }
        
        
    }else{
        
        [self loadOtherCont];
    }
    
    
}

- (void)loadOtherCont
{
    
    if (self.phoneTextF.text.length <=0 ){
        [NotifyLabel NotifyWithString:@"请输入手机号"];
    }else if (self.phoneTextF.text.length <=10 ){
        [NotifyLabel NotifyWithString:@"请输入正确的手机号"];
        return;
    }else if (![self.phoneTextF.text isEqualToString:[userDef valueForKey:userName]]) {
        [NotifyLabel NotifyWithString:@"该账号未注册,请前往注册!"];
        return;
    }else if (!self.SelectedBtn.isSelected){
        [NotifyLabel NotifyWithString:@"请勾选用户协议和隐私政策"];
        return;
    }
    else if (self.CodeTextF.text.length <=5 ){
        [NotifyLabel NotifyWithString:@"密码输入不正确"];
        return;
        
    }else if (![self.CodeTextF.text isEqualToString:[userDef valueForKey:userCode]]){
        [NotifyLabel NotifyWithString:@"密码输入不正确"];
        return;
    }
    [self setUserData];
    
}


- (void)setUserData {
    [userDef setValue:@"1" forKey:islogin];
    [userDef setValue:self.phoneTextF.text forKey:userName];
    [userDef setValue:self.CodeTextF.text forKey:userCode];
    
    [NotifyLabel NotifyWithString:@"登录成功"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self backBtnClick];
    });
}


- (void)registBtnDidClicked
{
    ZXZRegisterViewController *vc = [ZXZRegisterViewController new];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)SelectedBtnDidClicked:(UIButton *)button{
    button.selected = !button.selected;
}


@end
